package com.i2works.smartluolong.api.controller.index;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.i2works.smartluolong.api.utils.BaseModule;
import com.i2works.smartluolong.utils.entity.R;
import com.i2works.smartluolong.utils.entity.Result;
import com.i2works.smartluolong.utils.exception.ServiceException;
import com.i2works.smartluolong.utils.plugin.Pager;
import com.i2works.smartluolong.web.entity.Implement.ImplementDO;
import com.i2works.smartluolong.web.entity.Implement.ImplementFilesDO;
import com.i2works.smartluolong.web.entity.Implement.ImplementFilesQuery;
import com.i2works.smartluolong.web.entity.Implement.ImplementQuery;
import com.i2works.smartluolong.web.entity.common.SearchParam;
import com.i2works.smartluolong.web.service.implement.IImplementFilesService;
import com.i2works.smartluolong.web.service.implement.IImplementService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * app事项列表和统一申报接口
 *
 * @author Li haoyu
 * @date 2018/12/17
 */
@RestController
@RequestMapping("/itemList")
public class ItemListController extends BaseModule {
    @Autowired
    private IImplementService implementService;
    @Autowired
    private IImplementFilesService implementFilesService;
    /**
     * 个人事项
     */
    @GetMapping(value = {"/person"})
    public Result personList(@ModelAttribute SearchParam searchParam) {
        JSONObject jsonObject = new JSONObject();
        try {
            if (searchParam.getPage() == null || searchParam.getPage() == 0) {
                searchParam.setPage(1);
            }
            Integer pageNumber = searchParam.getPage();
            List<ImplementDO> itemsList = Lists.newArrayList();
            ImplementQuery itemsQuery = new ImplementQuery();
            itemsQuery.setStatus("Y");
            itemsQuery.setForUser("01");
            if(searchParam.getLimit()!=null){
                itemsQuery.setLimit(searchParam.getLimit());
            }
            if (StringUtils.isNotBlank(searchParam.getItemZT())) {
                itemsQuery.setPersonZt(searchParam.getItemZT());
            }
            if (StringUtils.isNotBlank(searchParam.getItemDX())) {
                itemsQuery.setPersonTdrq(searchParam.getItemDX());
            }
            if (StringUtils.isNotBlank(searchParam.getItemHD())) {
                itemsQuery.setPersonRssj(searchParam.getItemHD());
            }
            if (StringUtils.isNotBlank(searchParam.getDeptCode())) {
                itemsQuery.setDeptCode(searchParam.getDeptCode());
            }
            Integer queryCount = implementService.queryCount(itemsQuery);
            if (queryCount > 0) {
                itemsQuery.setOffset(itemsQuery.getLimit() * (pageNumber - 1));
                itemsList = implementService.queryList(itemsQuery);
            }

            //事项列表
            jsonObject.put("itemsList", itemsList);
            //事项面向对象
            jsonObject.put("itemType", "person");
            //个人/法人主题
            jsonObject.put("itemZT", StringUtils.isEmpty(searchParam.getItemZT()) ? null : searchParam.getItemZT());
            //个人特殊群体/法人对象
            jsonObject.put("itemDX", StringUtils.isEmpty(searchParam.getItemDX()) ? null : searchParam.getItemDX());
            //个人人生事件/法人经营活动
            jsonObject.put("itemHD", StringUtils.isEmpty(searchParam.getItemHD()) ? null : searchParam.getItemHD());
            //部门编码
            jsonObject.put("deptCode", StringUtils.isEmpty(searchParam.getDeptCode()) ? null : searchParam.getDeptCode());
            //分页实体
            jsonObject.put("pager", new Pager(pageNumber, queryCount, itemsQuery.getLimit().intValue()));
            return R.success(jsonObject);
        } catch (Exception e) {
            logger.error("ItemController.personList个人事项列表异常：" + e.getMessage());
            return R.error("个人事项列表异常");
        }
    }

    /**
     * 法人事项
     */
    @GetMapping(value = {"/legal"})
    public Result legalList(@ModelAttribute SearchParam searchParam) {
        JSONObject jsonObject = new JSONObject();
        try {
            if (searchParam.getPage() == null || searchParam.getPage() == 0) {
                searchParam.setPage(1);
            }
            Integer pageNumber = searchParam.getPage();
            List<ImplementDO> itemsList = Lists.newArrayList();
            ImplementQuery itemsQuery = new ImplementQuery();
            itemsQuery.setStatus("Y");
            itemsQuery.setForUser("02");
            if(searchParam.getLimit()!=null){
                itemsQuery.setLimit(searchParam.getLimit());
            }
            if (StringUtils.isNotBlank(searchParam.getItemDX())) {
                itemsQuery.setEnterpriseTddx(searchParam.getItemDX());
            }
            if (StringUtils.isNotBlank(searchParam.getDeptCode())) {
                itemsQuery.setDeptCode(searchParam.getDeptCode());
            }
            if (StringUtils.isNotBlank(searchParam.getItemZT())) {
                itemsQuery.setEnterpriseZt(searchParam.getItemZT());
            }
            if (StringUtils.isNotBlank(searchParam.getItemHD())) {
                itemsQuery.setEnterpriseJyhd(searchParam.getItemHD());
            }

            Integer queryCount = implementService.queryCount(itemsQuery);
            if (queryCount > 0) {
                itemsQuery.setOffset(itemsQuery.getLimit() * (pageNumber - 1));
                itemsList = implementService.queryList(itemsQuery);
            }
            //事项列表
            jsonObject.put("itemsList", itemsList);
            //事项
            jsonObject.put("itemType", "legal");
            //个人/法人主题
            jsonObject.put("itemZT", StringUtils.isEmpty(searchParam.getItemZT()) ? null : searchParam.getItemZT());
            //个人特殊群体/法人对象
            jsonObject.put("itemDX", StringUtils.isEmpty(searchParam.getItemDX()) ? null : searchParam.getItemDX());
            //个人人生事件/法人经营活动
            jsonObject.put("itemHD", StringUtils.isEmpty(searchParam.getItemHD()) ? null : searchParam.getItemHD());
            //部门编码
            jsonObject.put("deptCode", StringUtils.isEmpty(searchParam.getDeptCode()) ? null : searchParam.getDeptCode());
            //分页实体
            jsonObject.put("pager", new Pager(pageNumber, queryCount, itemsQuery.getLimit().intValue()));
            return R.success(jsonObject);
        } catch (Exception e) {
            logger.error("ItemController.legalList法人事项列表异常：" + e.getMessage());
            return R.error("法人事项列表异常");
        }
    }

    /**
     * 办事指南
     */
    @GetMapping(value = {"/detail/{unid}"})
    public Result detail(@PathVariable(value = "unid") String unid) {
        JSONObject jsonObject = new JSONObject();
        ImplementQuery itemsQuery = new ImplementQuery();
        itemsQuery.setUnid(unid);
        try {
            ImplementDO item = implementService.query(itemsQuery);
            ImplementFilesQuery filesQuery = new ImplementFilesQuery();
            filesQuery.setServiceId(item.getUnid());
            List<ImplementFilesDO> filesList = implementFilesService.queryList(filesQuery);
            jsonObject.put("filesList", filesList);
            jsonObject.put("item", item);
            return R.success(jsonObject);
        } catch (ServiceException e) {
            logger.error("ItemListController.detail办事指南异常：" + e.getMessage());
            return R.error("办事指南异常");
        }
    }

}
